<template>
  <div class="audio__wrap">
    <audio controls :src="src" ref="audioPlayer" style="display:none"></audio>
    <div class="self__audio" @click="playAudioHandler">
      <div class="audio__duration">{{ duration }}"</div>
      <div class="audio__trigger">
        <div :class="{'wifi-symbol': true, 'wifi-symbol--active': isPlaying}">
          <div class="wifi-circle second"></div>
          <div class="wifi-circle third"></div>
          <div class="wifi-circle first"></div>
        </div>
      </div>
    </div>
  </div>
</template>
<script>
import { ref, watchEffect, onMounted } from 'vue';

export default {
  props: {
    src: {
      type: String,
      required: true
    },
    value: {
      type: Boolean,
      required: true
    }
  },
  setup(props, { emit }) {
    const isPlaying = ref(false);
    const duration = ref('');
    const audioPlayer = ref(null);

    const playAudioHandler = () => {
      isPlaying.value = !isPlaying.value;
      const player = audioPlayer.value;
      if (isPlaying.value) {
        player.load();
        player.play();
      } else {
        player.pause();
      }
      setTimeout(() => {
        isPlaying.value = false;
        emit('input', false);
      }, (duration.value ? duration.value : 0) * 1000);
    };

    onMounted(() => {
      const player = audioPlayer.value;
      player.load();
      player.oncanplay = () => {
        duration.value = Math.ceil(player.duration);
      };
    });

    watchEffect(() => {
      if (!props.value) {
        isPlaying.value = false;
        audioPlayer.value.load();
      }
    });

    return {
      isPlaying,
      duration,
      audioPlayer,
      playAudioHandler
    };
  }
};
</script>

<style lang="scss" scoped>
.audio__wrap {
  .self__audio {
    .audio__duration {
      display: inline-block;
      line-height: 32px;
      height: 32px;
      padding-right: 6px;
      color: #888888;
    }
    .audio__trigger {
      cursor: pointer;
      vertical-align: top;
      display: inline-block;
      line-height: 32px;
      height: 32px;
      width: 100px;
      background-color: #e0effb;
      border-radius: 4px;
      position: relative;
      .wifi-symbol {
        position: absolute;
        right: 4px;
        top: -8px;
        width: 50px;
        height: 50px;
        box-sizing: border-box;
        overflow: hidden;
        transform: rotate(-45deg) scale(0.5);
        .wifi-circle {
          border: 5px solid #999999;
          border-radius: 50%;
          position: absolute;
        }

        .first {
          width: 5px;
          height: 5px;
          background: #cccccc;
          top: 45px;
          left: 45px;
        }
        .second {
          width: 25px;
          height: 25px;
          top: 35px;
          left: 35px;
        }
        .third {
          width: 40px;
          height: 40px;
          top: 25px;
          left: 25px;
        }
      }
      .wifi-symbol--active {
        .second {
          animation: bounce 1s infinite 0.2s;
        }
        .third {
          animation: bounce 1s infinite 0.4s;
        }
      }
    }
    @keyframes bounce {
      0% {
        opacity: 0;
      }
      100% {
        opacity: 1;
      }
    }
  }
}
</style>
